package com.android.update.intercept;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;

import com.alibaba.fastjson.JSON;
import com.android.update.config.HookDB;

import java.util.ArrayList;
import java.util.List;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class InterceptUtils {

    public static void authorization(XC_LoadPackage.LoadPackageParam lpparam) {
        String packName = lpparam.packageName;
        if (!HookDB.getIntance().getFilter().contains(packName)) {
            return;
        }

        //模拟手机的APP列表
        XposedHelpers.findAndHookMethod("android.app.ApplicationPackageManager", lpparam.classLoader, "getInstalledPackages", int.class,
                new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        List<PackageInfo> packageInfos = (List<PackageInfo>) param.getResult();
                        List<PackageInfo> newPackInfos = new ArrayList<>();
                        List<String> beFiltered = HookDB.getIntance().getBeFiltered();
                        String packageName;
                        for (PackageInfo info : packageInfos) {
                            packageName = info.packageName;
                            if (beFiltered.contains(packageName)) {
                                continue;
                            }
                            newPackInfos.add(info);
                        }
                        packageInfos.clear();
                        packageName = null;
                        packageInfos = null;
                        param.setResult(newPackInfos);
                    }
                });

        XposedHelpers.findAndHookMethod("android.app.ApplicationPackageManager", lpparam.classLoader, "getInstalledApplications", int.class,
                new XC_MethodHook() {

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        List<ApplicationInfo> apps = (List<ApplicationInfo>) param.getResult();
                        List<ApplicationInfo> news = new ArrayList<>();
                        List<String> beFiltered = HookDB.getIntance().getBeFiltered();
                        String packageName;
                        for (ApplicationInfo app : apps) {
                            packageName = app.packageName;
                            if (beFiltered.contains(packageName)) {
                                continue;
                            }
                            news.add(app);
                        }
                        apps.clear();
                        packageName = null;
                        apps = null;
                        param.setResult(news);
                    }
                });

    }

}
